package com.the_ring.chapter07;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.the_ring.chapter07.bean.BillInfo;
import com.the_ring.chapter07.database.DatabaseHelper;

import java.util.Calendar;

public class BillAddActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {

    private ImageView backIv;
    private TextView titleTv;
    private TextView actionTv;
    private TextView dateChoiceTv;
    private RadioGroup typeRg;
    private EditText detailEt;
    private EditText moneyEt;
    private Button saveBt;

    private BillInfo info;
    private Calendar calendar;

    private DatabaseHelper helper;

    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill_add);

        calendar = Calendar.getInstance();
        info = new BillInfo();
        info.date = getDateString(calendar);
        info.year = calendar.get(Calendar.YEAR);
        info.month = calendar.get(Calendar.MONTH) + 1;
        info.type = -1;

        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        helper = DatabaseHelper.getInstance(this);
        helper.openWriteLink();

        id = getIntent().getIntExtra("id", -1);
        if (id != -1) {
            BillInfo tmp = helper.queryById(id);
            if (tmp != null) {
                info = tmp;
                Log.d("BillAddActivity", info.detail);
                dateChoiceTv.setText(info.date);
                if (info.type == 1) {
                    ((RadioButton) findViewById(R.id.rb_input)).setChecked(true);
                } else {
                    ((RadioButton) findViewById(R.id.rb_output)).setChecked(true);
                }
                detailEt.setText(info.detail);
                moneyEt.setText(String.valueOf(info.money));

                calendar.set(Calendar.YEAR, info.year);
                calendar.set(Calendar.MONTH, info.month);
                calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(info.date.split("-")[2]));
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        helper.closeLink();
    }

    @SuppressLint("WrongViewCast")
    private void initView() {
        backIv = findViewById(R.id.iv_back);
        titleTv = findViewById(R.id.tv_title);
        actionTv = findViewById(R.id.tv_action);
        dateChoiceTv = findViewById(R.id.tv_date_choice);
        typeRg = findViewById(R.id.rg_type);
        detailEt = findViewById(R.id.et_detail);
        moneyEt = findViewById(R.id.et_money);
        saveBt = findViewById(R.id.bt_save);

        titleTv.setText("请填写账单");
        actionTv.setText("账单列表");
        dateChoiceTv.setText(getDateString(calendar));

        backIv.setOnClickListener(this);
        actionTv.setOnClickListener(this);
        dateChoiceTv.setOnClickListener(this);
        saveBt.setOnClickListener(this);
        typeRg.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_input) {
                info.type = 1;
            } else if (checkedId == R.id.rb_output) {
                info.type = -1;
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.tv_action) {
            Intent intent = new Intent(this, BillPagerActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        } else if (id == R.id.tv_date_choice) {
            DatePickerDialog dialog = new DatePickerDialog(this, this,
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH));
            dialog.show();
        } else if (id == R.id.bt_save) {
            String detail = detailEt.getText().toString();
            String money = moneyEt.getText().toString();
            if (detail.isEmpty() || money.isEmpty()) {
                Toast.makeText(this, "事项说明和金额不能为空！！", Toast.LENGTH_SHORT).show();
            } else {
                info.detail = detail;
                info.money = Integer.parseInt(money);
                save(info);
                Intent intent = new Intent(this, BillPagerActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        }
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        dateChoiceTv.setText(getDateString(calendar));

        info.date = getDateString(calendar);
        info.year = year;
        info.month = month;
    }

    public String getDateString(Calendar calendar) {
        return calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
    }

    public void save(BillInfo info) {
        BillInfo tmp = helper.queryById(info.id);
        if (tmp != null) {
            helper.update(info);
        } else {
            Log.i("BillAddActivity", "添加账单");
            helper.insert(info);
        }
    }
}